/**
 * Copyright (C) 2014-2015 5WeHealth Technologies. All rights reserved.
 */

package com.wehealth.electrocardiograph_two_client.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;


import com.wehealth.electrocardiograph_two_client.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 画出心电曲线图
 */
public class LineIntReadView extends View {
    private final String TAG = "LineView";
    private List<Point> mList1Point = new ArrayList<>();
    private List<Point> mList2Point = new ArrayList<>();
    private List<Point> mList3Point = new ArrayList<>();
    private final int LEADNUM = 3;
    private int baseY[] = new int[3];
    private Paint mPaint = new Paint();
    private Paint pacePaint = new Paint();
    private int WaveGain;  //波形增益
    private int paceMarker = 2147483647;
    private int paceMarkerShort = 32767;
    private int Margin_Left = 30;
    private int fenbianlv = 1;
    private final int MARGIN = 0;
    private Paint subGridPaint, topGridPaint;
    private int version = 0;
    private int heightPixels = 0;
    private int plotFactor = 105;

    public void setWaveGain(int value) {
        WaveGain = value;
    }



    public LineIntReadView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        Log.e(TAG, "LineIntReadView.super(context, attrs, defStyle)");
    }

    public LineIntReadView(Context context, AttributeSet attrs) {
        super(context, attrs);
        heightPixels = context.getResources().getDisplayMetrics().heightPixels;
        Log.e(TAG, "手机高度：" + heightPixels);
        Log.e(TAG, "LineIntReadView.super(context, attrs)");
    }

    public LineIntReadView(Context context) {
        super(context);
        Log.e(TAG, "LineIntReadView.super(context)");
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int heightPixels = getResources().getDisplayMetrics().heightPixels;

        int heightSize = MeasureSpec.getSize(heightMeasureSpec); //获取高的尺寸
        int widthSize;
        if (heightPixels <= 800) {
            widthSize = 1650;
        } else {
            widthSize = 2650;
        }

        Log.e(TAG, "LineIntReadView   onMeasure  w: " + widthSize + "   h: " + heightSize);
        setMeasuredDimension(widthSize, heightSize);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.e(TAG, "LineIntReadView     onDraw");
        if (heightPixels == 0) {
            heightPixels = getResources().getDisplayMetrics().heightPixels;
        }
        int viewHeight = getHeight();
        for (int i = 0; i < LEADNUM; i++) {
            baseY[i] = viewHeight / (LEADNUM * 2) * (2 * i + 1);
        }
        if (heightPixels <= 800) {
            fenbianlv = 1;
        } else if (heightPixels > 800) {
            fenbianlv = 2;
        }
        subGridPaint = new Paint();
        topGridPaint = new Paint();
        topGridPaint.setColor(getResources().getColor(R.color.ecg_back_blue));
        topGridPaint.setStrokeWidth((float) 1.5);
        subGridPaint.setColor(getResources().getColor(R.color.ecg_back_little));
        pacePaint.setColor(Color.RED);
        canvas.drawBitmap(drawBackground(30, getHeight(), getWidth(), canvas), 0, 0, null);

        mPaint.setColor(Color.GREEN);
        mPaint.setAntiAlias(true);

        if (version == 0) {
            plotFactor = 10;
            draw1Line(canvas, mList1Point);
            draw1Line(canvas, mList2Point);
            draw1Line(canvas, mList3Point);
        } else if (version == 1) {
            draw2Line(canvas, mList1Point);
            draw2Line(canvas, mList2Point);
            draw2Line(canvas, mList3Point);
        }
    }

    private void draw1Line(Canvas canvas, List<Point> mListPoint) {
        Point lastPoint = null;
        for (int index = 0; index < mListPoint.size(); index++) {
            Point currentPoint = mListPoint.get(index);
            if (index > 0) {
                if (currentPoint.y == paceMarkerShort) {
                    canvas.drawLine(currentPoint.x, lastPoint.y - 20,
                            currentPoint.x, lastPoint.y + 20, pacePaint);
                    canvas.setDrawFilter(new PaintFlagsDrawFilter(0,
                            Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
                    currentPoint = lastPoint;
                } else {
                    canvas.drawLine(lastPoint.x, lastPoint.y,
                            currentPoint.x, currentPoint.y, mPaint);
                    canvas.setDrawFilter(new PaintFlagsDrawFilter(0,
                            Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
                }

            }
            lastPoint = currentPoint;
        }
    }

    private void draw2Line(Canvas canvas, List<Point> mListPoint) {
        Point lastPoint = null;
        for (int index = 0; index < mListPoint.size(); index++) {
            Point currentPoint = mListPoint.get(index);
            if (index > 0) {
                if (currentPoint.y == paceMarker) {
                    canvas.drawLine(currentPoint.x, lastPoint.y - 20,
                            currentPoint.x, lastPoint.y + 20, pacePaint);
                    canvas.setDrawFilter(new PaintFlagsDrawFilter(0,
                            Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
                    currentPoint = lastPoint;
                } else {
                    canvas.drawLine(lastPoint.x, lastPoint.y,
                            currentPoint.x, currentPoint.y, mPaint);
                    canvas.setDrawFilter(new PaintFlagsDrawFilter(0,
                            Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
                }
            }
            lastPoint = currentPoint;
        }
    }

    /**
     * 绘制背景
     *
     * @param height
     * @param widht
     * @return
     */
    private Bitmap drawBackground(int cellSize, int height, int widht, Canvas canvas) {
        Bitmap bitmap = Bitmap.createBitmap(widht, height, Config.ARGB_8888);
        DrawVerticalLine(canvas, fenbianlv * 5, height, widht);
        DrawHorizontalLine(canvas, fenbianlv * 5, height, widht);
        return bitmap;
    }

    /**
     * 画水平线条
     **/
    private void DrawVerticalLine(Canvas canvas, int step, int screenHeight, int screenWidth) {
        int j = 0;
        for (int i = 0; i <= screenHeight; i += step) {
            if (j == 0) {
                canvas.drawLine(MARGIN,
                        i + MARGIN,
                        screenWidth + step,
                        i + MARGIN,
                        topGridPaint);
            } else {
                canvas.drawLine(MARGIN,
                        i + MARGIN,
                        screenWidth + step,
                        i + MARGIN,
                        subGridPaint);
            }
            j++;
            if (j >= 5)
                j = 0;
        }
    }

    /**
     * 画垂直线条
     **/
    private void DrawHorizontalLine(Canvas canvas, int step, int screenHeight, int screenWidth) {
        int j = 0;
        for (int i = 0; i <= screenWidth; i += step) {
            if (j == 0) {
                canvas.drawLine(i + MARGIN,
                        MARGIN,
                        i + MARGIN,
                        screenHeight + step,
                        topGridPaint);
            } else {
                canvas.drawLine(i + MARGIN,
                        MARGIN,
                        i + MARGIN,
                        screenWidth + step,
                        subGridPaint);
            }
            j++;
            if (j >= 5)
                j = 0;
        }
    }


    public void clearView() {
        mList1Point.clear();
        mList2Point.clear();
        mList3Point.clear();
        invalidate();
    }

    public void setVersion(int ecgData_version) {
        version = ecgData_version;
    }

}


